package com.app.view.manager 
{
	import flash.display.DisplayObjectContainer;
	import flash.utils.getQualifiedClassName;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import com.peachcastle.interfaces.IDialog;
	
	import com.app.view.component.dialogs.CreateRoom;
	import com.app.view.component.dialogs.SystemTipView;
	import com.app.view.component.dialogs.PkResultView;
	import com.app.view.component.dialogs.FeedBackView;
	import com.app.view.component.dialogs.ExchangeView;
	
	/**
	 * ...
	 * @author kevin
	 */
	public class DialogManagerMediator extends Mediator implements IMediator
	{
		public static const NAME:String 				= "DialogManagerMediator";
		public static const SET_DIALOG_LAYER:String 	= NAME + '_SetDialogLayer';
		public static const SET_STAGE_SIZE:String 		= NAME + '_SetStageSize';
		public static const SHOW_DIALOG:String 			= NAME + '_ShowDialog';
		public static const HIDE_DIALOG:String 			= NAME + '_HideDialog';
		
		//创建房间dialog//
		public static const CREATEROOM_SEND_INFO:String	= NAME + '_CreateRoomSendInfo';
		
		private var _manager:Object = null;
		private var _isPreloading:Boolean = false;
		protected var _preloadList:Array = null;
		
		private var _getDialogUrlFunc:Function = null;
		
		private var _stageSize:Object = {};
		private var _hallSize:Object = {};
		private var _roomSize:Object = {};
		
		public function DialogManagerMediator() 
		{
			super(NAME);
			
			_manager = {};
			_preloadList = ['createRoom', 'systemTipView', 'pkResultView', 'feedBackView', 'exchangeView'];
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				DialogManagerMediator.SET_DIALOG_LAYER,
				DialogManagerMediator.SET_STAGE_SIZE,
				DialogManagerMediator.SHOW_DIALOG,
				DialogManagerMediator.HIDE_DIALOG
			];
		}	
		
		override public function handleNotification(notification:INotification):void
		{
			var name:String = notification.getName();
			var param:* = notification.getBody();
			
			trace(name);

			switch ( name )
			{
				case DialogManagerMediator.SET_DIALOG_LAYER:
					layer = param[3];

					_getDialogUrlFunc 	= (param[0] as Function);
					
					_stageSize['width'] = param[1];
					stageSize['height'] = param[2];

					preloadRec();
			
					break;
					
				case DialogManagerMediator.SET_STAGE_SIZE:
					switch(param[0])
					{
						case "stage":
							_stageSize['width'] = param[1];
							_stageSize['height'] = param[2];
						
							break;
							
						case "HallScene":
							_hallSize['width'] = param[1];
							_hallSize['height'] = param[2];
						
							break;
							
						case "RoomScene":
							_roomSize['width'] = param[1];
							_roomSize['height'] = param[2];
						
							break;
					}
					
					break;
					
				case DialogManagerMediator.SHOW_DIALOG:
					switch(param[0])
					{
						case 'createRoom':
							createRoom.show(param[1]);
							
							break;
							
						case 'feedBackView':
							feedBackView.show(param[1]);
							
							break;
							
						case 'systemTipView':
							systemTipView.show(param[1]);
							
							break;
							
						case 'pkResultView':
							pkResultView.show(param[1]);
							
							break;
							
						case 'exchangeView':
							exchangeView.show(param[1]);
							
							break;
					}
			
					break;
					
				case DialogManagerMediator.HIDE_DIALOG:
					switch(param[0])
					{
						case 'createRoom':
							createRoom.hide();
							
							break;
							
						case 'feedBackView':
							feedBackView.hide();
							
							break;
							
						case 'systemTipView':
							systemTipView.hide();
							
							break;
							
						case 'pkResultView':
							pkResultView.hide();
							
							break;
							
						case 'exchangeView':
							exchangeView.hide();
							
							break;
					}
			
					break;
			}
		}						
		
		/**
		 * 预加载资源
		 */
		public function preloadRec():void
		{
			if (_isPreloading) return;
			_isPreloading = true;
			
			//
			var dialog:IDialog = null;
			var count:int = _preloadList.length;
			for (var i:int = 0; i < count; i++)
			{
				dialog = this[_preloadList[i]];
				dialog.preLoadRec();
				
				dialog = null;
			}
		}
		
		
		/**
		 * 创建对话框
		 * @param	DialogClass
		 */
		protected function getDialog(DialogClass:Class):*
		{
			var name:String = getQualifiedClassName(DialogClass);
			if (_manager[name] == null)
			{
				_manager[name] = new DialogClass(_getDialogUrlFunc, this);
				IDialog(_manager[name]).layer = layer;
			}
			
			//
			return _manager[name];
		}

		
		public function get createRoom():IDialog { return getDialog(CreateRoom); }
		public function get systemTipView():IDialog { return getDialog(SystemTipView); }
		public function get pkResultView():IDialog { return getDialog(PkResultView); }
		public function get feedBackView():IDialog { return getDialog(FeedBackView); }
		public function get exchangeView():IDialog { return getDialog(ExchangeView); }
		
		private var _layer:DisplayObjectContainer = null;
		public function get layer():DisplayObjectContainer { return _layer; }
		public function set layer(value:DisplayObjectContainer):void { _layer = value; }		
		
		public function get stageSize():Object { return _stageSize; }
		public function get hallSize():Object { return _hallSize; }
		public function get roomSize():Object { return _roomSize; }
	}

}